package cn.saltybe.analysis.service;

import cn.saltybe.analysis.entity.result.UniqueVisitor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UniqueVisitorServiceImpl implements UniqueVisitorService {
    private JdbcTemplate jdbcTemplate;

    // set方法注入JdbcTemplate
    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) throws Exception {
        this.jdbcTemplate = jdbcTemplate;
        this.createTable();
    }

    private final BeanPropertyRowMapper<UniqueVisitor> rowMapper = new BeanPropertyRowMapper<>(UniqueVisitor.class);

    public void createTable() throws Exception {
        if (this.jdbcTemplate == null) throw new Exception("ERR_INIT_DRIVER_FAILED");
        final String sql = "CREATE TABLE IF NOT EXISTS `unique_visitor` (" +
                "`time` DATETIME NOT NULL," +
                "`count` DOUBLE NOT NULL" +
                ") ENGINE = InnoDB CHARSET = utf8mb4";
        this.jdbcTemplate.execute(sql);
    }

    public void dropTable() throws Exception {
        if (this.jdbcTemplate == null) throw new Exception("ERR_INIT_DRIVER_FAILED");
        final String sql = "DROP TABLE `unique_visitor`";
        this.jdbcTemplate.execute(sql);
    }

    @Override
    public void insert(UniqueVisitor uniqueVisitor) throws Exception {
        if (this.jdbcTemplate == null) throw new Exception("ERR_INIT_DRIVER_FAILED");
        final String sql = "INSERT INTO `unique_visitor` (`time`, `count`) VALUES (?, ?)";
        Object[] params = new Object[]{uniqueVisitor.getTime(), uniqueVisitor.getCount()};
        jdbcTemplate.update(sql, params);
    }

    @Override
    public List<UniqueVisitor> all() throws Exception {
        if (this.jdbcTemplate == null) throw new Exception("ERR_INIT_DRIVER_FAILED");
        final String sql = "SELECT * FROM `unique_visitor`";
        List<UniqueVisitor> list = this.jdbcTemplate.query(sql, rowMapper);
        return list;
    }

    @Override
    public List<UniqueVisitor> query(Date time) throws Exception {
        if (this.jdbcTemplate == null) throw new Exception("ERR_INIT_DRIVER_FAILED");
        final String sql = "SELECT * FROM `unique_visitor` WHERE `time` = ?";
        Object[] params = new Object[]{ new Timestamp(time.getTime()) };
        List<UniqueVisitor> list = this.jdbcTemplate.query(sql, rowMapper, params);
        return list;
    }

    @Override
    public int delete(Date time) throws Exception {
        if (this.jdbcTemplate == null) throw new Exception("ERR_INIT_DRIVER_FAILED");
        final String sql = "DELETE FROM `unique_visitor` WHERE `time` = ?";
        Object[] params = new Object[]{ new Timestamp(time.getTime()) };
        return this.jdbcTemplate.update(sql, params);
    }
}
